<?php
$stack =[];
function generate ($left, $right, $path='', &$arr=[])
{ global $stack;
  array_push($stack, "{$left}-{$right}");
  if ($left ==0 && $right ==0) {
  	array_push($arr, $path);
  	var_dump($arr);
  	return;
  }
  if ($left != 0) {
  	echo "left 1:{$left}-{$right}\n";
  	generate($left-1,$right,$path.'(',$arr);
  	echo "left 2:{$left}-{$right}\n";
  }
  if($right !=0 && $left < $right)
  {
  	echo "right1:{$left}-{$right} \n";
  	generate($left, $right-1,$path.')',$arr);
  	echo "right2:{$left}-{$right}\n";
  }
}
$arr = [];
generate(2,2, '', $arr);
var_dump($stack);